package com.jt.blog.controller;

import com.jt.blog.domain.Tag;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

@RestController
public class TagController {

    List<Tag> tags = new CopyOnWriteArrayList<>();
    public void test(){
//        tags.set()
    }
    /*spring中bean对象的生命周期方法，对象初始化时执行定时任务*/
    @PostConstruct
    public void doInit(){
        TimerRefreshTask();
    }
    /*spring中bean对象的生命周期方法，对象销毁时执行此方法*/
    @PreDestroy
    public void doDestroy(){
        //关闭定时任务
        timer.cancel();
    }
    /**
     * 启动定时任务的方法
     */
    private Timer timer;
    private void TimerRefreshTask(){
        //构建一个定时任务调度对象
        timer = new Timer();
        //构建一个任务对象
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                tags.clear();
            }
        };
        timer.schedule(task, 5000,6000);
    }
}
